package org.dromara.capital.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.dromara.admin.domain.bo.UserDetailBo;
import org.dromara.admin.domain.vo.UserDetailVo;
import org.dromara.admin.model.UserPasswordBo;
import org.dromara.admin.service.IUserDetailService;
import org.dromara.admin.tenant.controller.TenantUserDetailController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.file.MimeTypeUtils;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysUserPasswordBo;
import org.dromara.system.domain.bo.SysUserProfileBo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

/**
 * 资方用户个人信息
 *
 * @author Lion Li
 * @date 2025-09-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/capital/userDetail")
public class CapitalUserDetailController extends BaseController {

    private final IUserDetailService userDetailService;
    private final ISysOssService ossService;

    /**
     * 资方获取自己的个人信息
     */
//    @SaCheckPermission("capital:userDetail:query")
    @GetMapping("/getMyInfo")
    public R<UserDetailVo> getMyInfo() {
        Long userId = LoginHelper.getUserId();
        return R.ok(userDetailService.queryById(userId));
    }

    /**
     * 资方修改用户详细
     */
//    @SaCheckPermission("capital:userDetail:edit")
    @Log(title = "用户详细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserDetailBo bo) {
        return toAjax(userDetailService.updateByBo(bo));
    }

    /**
     * 出租方修改密码
     */
//    @SaCheckPermission("capital:userDetail:resetPwd")
    @Log(title = "用户详细", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public R<Void> resetPwd(@RequestBody UserPasswordBo userPasswordBo) {
        if (StrUtil.isEmpty(userPasswordBo.getNewPassword())) {
            return R.fail("请输入密码！");
        }
        // 使用当前登录用户，修改当前登录用户的密码
        userPasswordBo.setUserId(LoginHelper.getUserId());
        userPasswordBo.setNewPassword(BCrypt.hashpw(userPasswordBo.getNewPassword()));
        return toAjax(userDetailService.resetUserPwd(userPasswordBo.getUserId(), userPasswordBo.getNewPassword()));
    }

    /**
     * 头像上传
     *
     * @param avatarfile 用户头像
     */
//    @SaCheckPermission("capital:userDetail:edit")
    @RepeatSubmit
    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/avatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<TenantUserDetailController.AvatarVo> avatar(@RequestPart("avatarfile") MultipartFile avatarfile) {
        if (!avatarfile.isEmpty()) {
            String extension = FileUtil.extName(avatarfile.getOriginalFilename());
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
                return R.fail("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
            }
            SysOssVo oss = ossService.upload(avatarfile);
            Boolean updateSuccess = userDetailService.updateUserAvatar(LoginHelper.getUserId(), oss);
            String avatar = oss.getUrl();
            if (updateSuccess) {
                return R.ok(new TenantUserDetailController.AvatarVo(avatar));
            }
        }
        return R.fail("上传图片异常，请联系管理员");
    }
}
